package org.eae.eldf.webapp.controller.sys;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.entity.sys.SysMenu;
import org.eae.eldf.entity.sys.SysPurview;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.sys.SysMenuService;
import org.eae.eldf.service.sys.SysPurviewService;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

@Controller
public class SysMenuController extends AbstractController<SysMenu> {

	private SysMenuService menuService;

	private SysPurviewService purviewService;

	private EhCacheCacheManager cacheManager;

	@Autowired
	public void setCacheManager(EhCacheCacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

	@RequestMapping(value = "/sys/menu/list")
	public String listPage(@RequestParam(required = false) Long parentid, ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		return "/sys/menu/sys_menu";
	}

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/sys/menu/menu_list")
	public @ResponseBody List<SysMenu> pageList(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "sysMenu") SysMenu sysMenu) {

		return menuService.queryMenuList(sysMenu);
	}

	/**
	 * 跳转到菜单表单页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/sys/menu/form")
	@Token(save = true)
	public String toFormPage(@RequestParam(required = false) Long id, ModelMap model,
			@RequestParam(required = false) Long parentid, @RequestParam(required = false) String sorting_column_order,
			@RequestParam(required = false) String sorting_asc_desc) {
		SysMenu menu = null;
		if (null != id) {
			menu = menuService.get(SysMenu.class, id);
		} else {
			menu = new SysMenu();
			if (parentid != null) {
				model.addAttribute("parent_menu", menuService.get(SysMenu.class, parentid));
			}
		}
		model.addAttribute("menu", menu);
		model.addAttribute("param_parentId", parentid);
		model.addAttribute("sorting_column_order", sorting_column_order);
		model.addAttribute("sorting_asc_desc", sorting_asc_desc);
		return "/sys/menu/form";
	}

	/**
	 * 新增菜单
	 * 
	 * @param operator
	 * @return
	 */
	@RequestMapping(value = "/sys/menu/add")
	@ResponseBody
	public Result insert(HttpServletRequest request, @ModelAttribute(value = "sysMenu") SysMenu sysMenu) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		menuService.insert(Criteria.create(SysMenu.class).set("sname_en", sysMenu.getSname_en())
				.set("sname", sysMenu.getSname()).set("smenupath", sysMenu.getSmenupath())
				.set("isort", sysMenu.getIsort()).set("sremark", sysMenu.getSremark())
				.set("siconcls", sysMenu.getSiconcls()).set("bisroot", 0).set("bisleaf", 1).set("dadddate", new Date())
				.set("dcreatetime", new Date()).set("dlastupdatetime", new Date()).set("bisdisplay", 1)
				.set("imenulevel", 0).set("slastupdateuser", SpringSecurityUtils.getCurrentUserName())
				.set("parentid", sysMenu.getParentid()).set("simagepath", sysMenu.getSimagepath()));
		return result;
	}

	/**
	 * 新增菜单
	 * 
	 * @param operator
	 * @return
	 */
	@RequestMapping(value = "/sys/menu/update")
	@ResponseBody
	public Result update(HttpServletRequest request, @ModelAttribute(value = "sysMenu") SysMenu sysMenu) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		menuService.update(Criteria.create(SysMenu.class).set("sname_en", sysMenu.getSname_en())
				.set("sname", sysMenu.getSname()).set("smenupath", sysMenu.getSmenupath())
				.set("isort", sysMenu.getIsort()).set("sremark", sysMenu.getSremark())
				.set("siconcls", sysMenu.getSiconcls()).set("bisroot", 0).set("bisleaf", 1).set("dadddate", new Date())
				.set("simagepath", sysMenu.getSimagepath()).set("dlastupdatetime", new Date()).set("bisdisplay", 1)
				.set("imenulevel", 0).set("slastupdateuser", SpringSecurityUtils.getCurrentUserName())
				.where("sysMenuId", new Object[] { sysMenu.getSysMenuId() }));
		return result;
	}

	/**
	 * 删除菜单
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/sys/menu/del")
	public @ResponseBody Result delete(HttpServletRequest request,@RequestParam(required = false) Long id) {
		
		SysMenu sysMenu=menuService.get(SysMenu.class, id);
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		RequestContext requestContext = new RequestContext(request);
		try {
			int count = menuService.queryCount(Criteria.create(SysMenu.class).where("parentid", new Object[] { sysMenu.getSysMenuId() }));
			if (count > 0) {
				
				result.setResultValue(false);
				result.setMessage(requestContext.getMessage("sys.menu.exist.next.menu"));
				// 有子节点 不能删除
				return result;
			}
			int precount = purviewService
					.queryCount(Criteria.create(SysPurview.class).where("parentid", new Object[] { sysMenu.getSysMenuId() }));
			if (precount > 0) {
				result.setResultValue(false);
				result.setMessage(requestContext.getMessage("sys.menu.exist.next.purview"));
				// 有对应的权限 不能删除
				return result;
			}
			menuService.delete(SysMenu.class, sysMenu.getSysMenuId());
			if (sysMenu.getParentid() != -1) {
				// 父菜单下还有没有子节点 如果没有 更新父节点bisleaf=1
				if (menuService
						.queryCount(Criteria.create(SysMenu.class).where("parentid", new Object[] { sysMenu.getParentid() })) == 0) {
					menuService.update(Criteria.create(SysMenu.class).set("bisleaf", 1).where("sysMenuId",
							new Object[] { sysMenu.getParentid() }));
				}
			}
			cacheManager.getCacheManager().getCache("baseCache").remove("operator_all_menus");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Autowired
	public void setPurviewService(SysPurviewService purviewService) {
		this.purviewService = purviewService;
	}

	@Autowired
	public void setMenuService(SysMenuService menuService) {
		this.menuService = menuService;
	}

}
